<?php
namespace Controller;

use Illuminate\Database\Capsule\Manager;
use Slim\Http\Response;
use Slim\Slim;


class Index
{

    public $renderer;
    public $redis;
    public $logger;
    public $user;
    public $id;

    public function __construct()
    {


        $this->renderer = Slim::getInstance()->container->get('renderer');


    }

    public function article()
    {
        $data = array();

        $article = Manager::table('article')
            ->orderBy('num','desc')
            ->orderBy('updatetime','desc')
            ->get()
            ->toArray();
        foreach ($article as $val) {
            $data[] = (Array)$val;
        }
        $this->renderer->assign('article', $data);
        $this->renderer->display('index.html');
    }

    public function doarticle()
    {
        $id = intval($_POST['id']);
        if (empty($id)) {
            return new Response('data error', 400);
            exit();
        } else {
            $articleobj = Manager::table('article')->find($id);
            if ($articleobj) {
                $saved = Manager::table('article')
                    ->where('id', '=', $id)
                    ->update(array(
                        'num' => $articleobj->num + 1,
                        'updatetime' => time()
                    ));
                if ($saved) {
                    return new Response('data success');
                    exit();
                } else {
                    return new Response('data error', 400);
                    exit();
                }
            } else {
                return new Response('data error', 400);
                exit();
            }

        }


    }

    public function savearticle()
    {
        $execlpath = SYSTEM_PATH . '/qa/qa.xlsx';

        // 读取excel文件
        try {
            $inputFileType = \PHPExcel_IOFactory::identify($execlpath);
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($execlpath);
            // 确定要读取的sheet，什么是sheet，看excel的右下角，真的不懂去百度吧
            $sheet = $objPHPExcel->getSheet(0);
            $highestRow = intval($sheet->getHighestDataRow());



// 获取一行的数据


            for ($row = 2; $row <= $highestRow; $row++) {

                if ($row % 2 == 0) {
                    $qcontent = $sheet->getCellByColumnAndRow(2, $row)->getValue();
                    $this->id = Manager::table('article')->insertGetId(
                        array('qcontent' => $qcontent,
                            'createtime' => time(),
                            'updatetime' => time())
                    );


                } else {
                    $acontent = $sheet->getCellByColumnAndRow(2, $row)->getValue();
                    $saved = Manager::table('article')
                        ->where('id', '=', $this->id)
                        ->update(array(
                            'acontent' => $acontent,
                            'updatetime' => time()
                        ));
                    if ($saved) {
                       unset($this->id);continue;
                    } else {
                        break;
                    }
                }


            }
        } catch (\Exception $e) {
            die('加载文件发生错误：' . $e->getMessage());
        }


    }


}